package cn.edu.lingnan.translation.service.impl;

import cn.edu.lingnan.translation.entity.Menu;
import cn.edu.lingnan.translation.entity.vo.MenuParent;
import cn.edu.lingnan.translation.mapper.MenuMapper;
import cn.edu.lingnan.translation.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Bao
 * @since 2020-10-14
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     * @Description TODO
     * @Param []
     * @Return cn.edu.lingnan.translation.entity.Menu
     * @Author BAO
     * @Date 2020/10/14 11:09
     */
    @Override
    public List<MenuParent> getAllMenu() {
        // 获取所有一级菜单信息
        QueryWrapper<Menu> parentWrapper = new QueryWrapper<>();
        parentWrapper.eq("parent_mid", 0);
        List<Menu> pMenus = baseMapper.selectList(parentWrapper);

        // 获取所有子级菜单信息
        QueryWrapper<Menu> sonWrapper = new QueryWrapper<>();
        parentWrapper.ne("parent_mid", 0);
        List<Menu> sMenus = baseMapper.selectList(sonWrapper);

        // 封装json格式对象
        List<MenuParent> list = new ArrayList<>();
        for (Menu m : pMenus) {
            MenuParent menuParent = new MenuParent();
            BeanUtils.copyProperties(m, menuParent);
            // 封装一级菜单
            list.add(menuParent);

            // 将二级菜单封装进一级菜单
            List<Menu> smList = new ArrayList<>();
            for (Menu sm : sMenus) {
                if (m.getMid().equals(sm.getParentMid())) {
                    smList.add(sm);
                }
            }
            menuParent.setChildren(smList);
        }
        return list;
    }
}
